package com.employee.seckill.util;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * ClassName: Result
 * Package: com.employee.seckill.util
 * Description:统一返回体
 *
 * @Author 刘荟雅
 * @Create 2025/9/11 15:35
 * @Version 1.0
 */

@Data
@NoArgsConstructor
@Schema(description = "统一响应结果")
public class Result<T> {
    private int code;   // 200 成功  500 失败
    private String msg;
    private T data;

    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(200, "success", data);   // 3 个参数
    }
    // 带 data 的 error，用于支持类型推断
    public static <T> Result<T> error(String msg, T data) {
        return new Result<>(500, msg, data);
    }

    // 简化版 error，如果不需要 data
    public static <T> Result<T> error(String msg) {
        return new Result<>(500, msg, null);
    }

}